package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrgroupWithAttrVos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-31 22:55:50
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    ///product/attrgroup/{catelogId}/withattr
    /**
     * 获取分类id下的所有分组信息和分组下的所有基本信息
     */
    @GetMapping("/{catelogId}/withattr")
    public R attrgroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrgroupWithAttrVos> attrVos = attrGroupService.selectGroupWithAttr(catelogId);
        return R.ok().put("data",attrVos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组的所有属性
     * @param attrgroupId
     * @return
     */
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntity = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntity);
    }

    /**
     * 删除分组关联关系
     * @param vos
     * @return
     */
    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation/delete")
    public R addRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 获取所有未关联的属性
     * @param categoryId
     * @param parms
     * @return
     */
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long categoryId,
                            @RequestParam Map<String,Object> parms){
       PageUtils page =  attrService.getNoRelationAttr(parms,categoryId);
       return R.ok().put("page",page);
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R attrAddRelation(@RequestBody List<AttrGroupRelationVo> vos){
         relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping(" ")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//获取分类id
        Long catelogId = attrGroup.getCatelogId();
        //根据分类id查找分类id路径
       Long[] path = categoryService.queryCategoryIdPath(catelogId);
       attrGroup.setCatelogIdPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     * @param attrGroupIds
     * @return
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
